/* psr.c */

#include <stdlib.h>
#include <gtk/gtk.h>
#include <hildon/hildon-program.h>

typedef enum {
    MENU_GAME_PAPER = 1,
    MENU_GAME_SCISSORS = 2,
    MENU_GAME_ROCK = 3,
    MENU_GAME_QUIT = 4
} MenuActionCode;

static gboolean delete_event(GtkWidget* widget, GdkEvent event,
	gpointer data) {
    return FALSE;
}

static void end_program(GtkWidget* widget, gpointer data) {
    gtk_main_quit();
}

int main(int argc, char** argv) {
    HildonProgram* program;
    HildonWindow* window;
    GtkWidget* playerLabel;
    GtkWidget* chooseLabel;
    GtkWidget* cpuLabel;
    GtkWidget* winnerLabel;
    GtkWidget* topbox;
    GtkWidget* mainbox;
    GtkWidget* buttonbox;
    GtkWidget* bQuit;
    GtkWidget* bPaper;
    GtkWidget* bScissors;
    GtkWidget* bRock;
    GtkWidget* separator;

    gtk_init(&argc, &argv);

    static void playGame(GtkWidget* b, gpointer data) {
	int player = GPOINTER_TO_INT(data);
	srand(time(0));

	switch (player) {
	    case 1:
		g_object_set(playerLabel, "label", "You: Paper", NULL);
		break;
	    case 2:
		g_object_set(playerLabel, "label", "You: Scissors", NULL);
		break;
	    case 3:
		g_object_set(playerLabel, "label", "You: Rock", NULL);
		break;
	}

	int cpu = 0;
	int cpu1 = rand();

	int range1 = RAND_MAX / 3;
	int range2 = range1 * 2;
	
	if (cpu1 <= range1) {
	  g_object_set(cpuLabel, "label", "CPU: Paper", NULL);
	  cpu = 1;
	} else if (cpu1 > range1 && cpu1 <= range2) {
	  g_object_set(cpuLabel, "label", "CPU: Scissors", NULL);
	  cpu = 2;
	} else {
	  g_object_set(cpuLabel, "label", "CPU: Rock", NULL);
	  cpu = 3;
	}

        if (player == cpu) {
	  g_object_set(winnerLabel, "label", "It's a Tie!", NULL);
	} else if ((player == 1 && cpu == 3) || (player == 2 && cpu == 1) || (player == 3 && cpu == 2)) {
	  g_object_set(winnerLabel, "label", "You win!", NULL);
	} else if ((player == 1 && cpu == 2) || (player == 2 && cpu == 3) || (player == 3 && cpu == 1)) {
	  g_object_set(winnerLabel, "label", "CPU wins!", NULL);
	}
    }
    
    program = HILDON_PROGRAM(hildon_program_get_instance());
    g_set_application_name("Paper Scissors Rock");
    window = HILDON_WINDOW(hildon_window_new());
    hildon_program_add_window(program, HILDON_WINDOW(window));
    

    chooseLabel = g_object_new(GTK_TYPE_LABEL,
	    "label", "Choose your hand!",
	    NULL);
    playerLabel = g_object_new(GTK_TYPE_LABEL,
	    "label", "YOU: ",
	    NULL);
    cpuLabel = g_object_new(GTK_TYPE_LABEL,
	    "label", "CPU: ",
	    NULL);
    winnerLabel = g_object_new(GTK_TYPE_LABEL,
	    "label", "Who wins?",
	    NULL);
    bQuit = g_object_new(GTK_TYPE_BUTTON,
	    "label", "Quit!",
	    NULL);
    bPaper = g_object_new(GTK_TYPE_BUTTON,
	    "label", "Paper",
	    NULL);
    bScissors = g_object_new(GTK_TYPE_BUTTON,
	    "label", "Scissors",
	    NULL);
    bRock = g_object_new(GTK_TYPE_BUTTON,
	    "label", "Rock",
	    NULL);

      topbox = g_object_new(GTK_TYPE_VBOX, NULL);
      mainbox = g_object_new(GTK_TYPE_VBOX, NULL);
      buttonbox = g_object_new(GTK_TYPE_HBUTTON_BOX, NULL);
      separator = gtk_hseparator_new();
      
      g_object_set(window, "child", topbox, NULL);

      /* putting the mainbox into topbox and adding stuff to mainbox */
      gtk_box_pack_start(GTK_BOX(topbox), GTK_WIDGET(mainbox), TRUE, TRUE, 0);

      gtk_box_pack_start(GTK_BOX(mainbox), GTK_WIDGET(chooseLabel), TRUE, TRUE, 0);
      gtk_box_pack_start(GTK_BOX(mainbox), GTK_WIDGET(playerLabel), TRUE, TRUE, 0);
      gtk_box_pack_start(GTK_BOX(mainbox), GTK_WIDGET(cpuLabel), TRUE, TRUE, 0);
      gtk_box_pack_start(GTK_BOX(mainbox), GTK_WIDGET(winnerLabel), TRUE, TRUE, 0);
      
      /* putting the buttonbox, after the separator into topbox and adding buttons */
      gtk_box_pack_start(GTK_BOX(topbox), GTK_WIDGET(separator), FALSE, TRUE, 10);
      gtk_box_pack_end(GTK_BOX(topbox), GTK_WIDGET(buttonbox), FALSE, TRUE, 0);

      gtk_box_pack_start(GTK_BOX(buttonbox), GTK_WIDGET(bPaper), TRUE, TRUE, 0);
      gtk_box_pack_start(GTK_BOX(buttonbox), GTK_WIDGET(bScissors), TRUE, TRUE, 0);
      gtk_box_pack_start(GTK_BOX(buttonbox), GTK_WIDGET(bRock), TRUE, TRUE, 0);
      gtk_box_pack_start(GTK_BOX(buttonbox), GTK_WIDGET(bQuit), TRUE, TRUE, 0);
      
      /* lots of events */
      g_signal_connect(G_OBJECT(window), "delete-event",
	      G_CALLBACK(delete_event), NULL);
      g_signal_connect(G_OBJECT(window), "destroy",
	      G_CALLBACK(end_program), NULL);
      g_signal_connect(G_OBJECT(bQuit), "clicked",
	      G_CALLBACK(end_program), NULL);
      g_signal_connect(G_OBJECT(bPaper), "clicked",
	      G_CALLBACK(playGame), GINT_TO_POINTER(MENU_GAME_PAPER));
      g_signal_connect(G_OBJECT(bScissors), "clicked",
	      G_CALLBACK(playGame), GINT_TO_POINTER(MENU_GAME_SCISSORS));
      g_signal_connect(G_OBJECT(bRock), "clicked",
	      G_CALLBACK(playGame), GINT_TO_POINTER(MENU_GAME_ROCK));
      
      gtk_widget_show_all(GTK_WIDGET(window));

      gtk_main();
      return EXIT_SUCCESS;
}
